package com.yangli.leecode.mashib.code;

/**
 * @Description
 * @Author liyang
 * @Date 2023/7/17 9:52
 */
public class MiddleClass_03 {
    public static void main(String[] args){
    }

    public static int maxLength(String s){
        if (s == null || s.equals("")) {
            return 0;
        }
        char[] str = s.toCharArray();
        int[] dp = new int[s.length()];
        int pre = 0;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (str[i] == ')') {
                pre = i - dp[i - 1] - 1;//str[i]配对左括号的位置
                if (pre >= 0 && str[pre] == '(') {
                    dp[i] = dp[i - 1] + 2 + (pre > 0 ? dp[pre - 1] : 0);
                }
                res = Math.max(res, dp[i]);
            }
        }
        return res;
    }

}
